package com.hongsen.cn.presenter;

import com.hongsen.cn.Utils.GsonUtils;
import com.hongsen.cn.Utils.RxBus;
import com.hongsen.cn.application.DayAndMonthApplication;
import com.hongsen.cn.base.BaseModel;
import com.hongsen.cn.base.BaseObserver;
import com.hongsen.cn.base.BasePresenter;
import com.hongsen.cn.bean.BaseTitleBean;
import com.hongsen.cn.bean.FirstPageFaceCallBackBean;
import com.hongsen.cn.bean.FirstPageFaceListBean;
import com.hongsen.cn.bean.HaveNotDoneWorkBean;
import com.hongsen.cn.bean.NativeBean;
import com.hongsen.cn.bean.OrderBean;
import com.hongsen.cn.bean.PoliceBean;
import com.hongsen.cn.bean.RxValue;
import com.hongsen.cn.bean.WorkPlanBean;
import com.hongsen.cn.contract.HomeView;

import java.util.List;

import static com.hongsen.cn.bean.RxBusEventKey.FINISH_UNCLICK_ACTIVITY;

public class HomePresenter extends BasePresenter<HomeView> {

    public HomePresenter(HomeView baseView) {
        super(baseView);
    }

    public String name ;

    /**
     * 角色预约
     */
    public void getOrderList(final String type, final String bodyId, final String policeId) {
        addDisposable(restApi.getOrderList(type, bodyId, policeId), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<List<OrderBean>> model = (BaseModel<List<OrderBean>>) o;
                baseView.getOrderList(type, bodyId, model.getData());

                switch (bodyId){
                    case "0":
                        name = "医生";
                        break;
                    case "1":
                        name = "民警";
                        break;
                    case "2":
                        name = "所领导";
                        break;
                    case "4":
                        name = "检察官";
                        break;
                        default:
                }

                DayAndMonthApplication.getInstance().logger.debug(">>>根据角色" + name + "获取未完成" + type + "工作:" +
                        GsonUtils.getInstance().toJson(model));
            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                switch (bodyId){
                    case "0":
                        name = "医生";
                        break;
                    case "1":
                        name = "民警";
                        break;
                    case "2":
                        name = "所领导";
                        break;
                    case "4":
                        name = "检察官";
                        break;
                    default:
                }

                DayAndMonthApplication.getInstance().logger.debug(">>>根据角色" + name + "获取未完成" + type + "工作失败:" +
                        msg);
            }
        });
    }

    /**
     * 所内通知
     */
    public void getNotiveList(final int current, final int page) {
        addDisposable(restApi.getNotiveList(current, page), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<NativeBean> model = (BaseModel<NativeBean>) o;
                NativeBean nativeBean = model.getData();
                baseView.getNotiveList(nativeBean.getRecords());
                DayAndMonthApplication.getInstance().logger.debug(">>>所内通知:" + GsonUtils.getInstance().toJson(model));

            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                DayAndMonthApplication.getInstance().logger.debug(">>>所内通知获取失败:" + msg);

            }
        });
    }


    /**
     * 民警人脸抓拍登录
     * 12396300
     */
    public void login(String policeId) {
        addDisposable(restApi.login(policeId), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<PoliceBean> model = (BaseModel<PoliceBean>) o;
                baseView.getLogiInfo(model.getData());
                DayAndMonthApplication.getInstance().logger.debug(">>>ID登录成功login:" + GsonUtils.getInstance().toJson(model));
                if (model.getData() != null) {
                    RxBus.getDefault().post(new RxValue(FINISH_UNCLICK_ACTIVITY, "finish"));
                }
            }

            @Override
            public void onError(String msg) {
                DayAndMonthApplication.getInstance().logger.debug(">>>ID登录失败login:" + msg);
            }
        });
    }

    /**
     * 获取今日未处理工作
     */
    public void getHaveNotDoneWork() {
        addDisposable(restApi.getAllRoleStatistical(), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<HaveNotDoneWorkBean> model = (BaseModel<HaveNotDoneWorkBean>) o;
                DayAndMonthApplication.getInstance().logger.debug(">>>今日未处理工作getAllRoleStatistical:" + GsonUtils.getInstance().toJson(model));
                baseView.getHaveNotDoneWork(model.getData());
            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                DayAndMonthApplication.getInstance().logger.debug(">>>今日未处理工作获取失败getAllRoleStatistical:" + msg);
                getWorkPlan();
            }
        });
    }

    /**
     * 获取所有工作进度
     */
    public void getWorkPlan() {
        addDisposable(restApi.getAllPolicePercentage(), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<List<WorkPlanBean>> model = (BaseModel<List<WorkPlanBean>>) o;
                DayAndMonthApplication.getInstance().logger.debug(">>>获取工作进度getAllPolicePercentage成功:" + GsonUtils.getInstance().toJson(model));
                baseView.getWorkPlan(model.getData());
            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                DayAndMonthApplication.getInstance().logger.debug(">>>获取工作进度getAllPolicePercentage失败:" + msg);
                getTitleInfo();
            }
        });
    }

    /**
     * 今日违规、加戴械具、动态人员、出所人员数量
     */
    public void getTitleInfo() {
        addDisposable(restApi.getTopPersonalData(), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                DayAndMonthApplication.getInstance().logger.debug(">>>刷新getTopPersonalData:" + (String) GsonUtils.getInstance().toJson((BaseModel) o));

                BaseModel<BaseTitleBean> model = (BaseModel<BaseTitleBean>) o;
                baseView.getTitleInfo(model.getData());
            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                DayAndMonthApplication.getInstance().logger.debug(">>>getTopPersonalData数据失败:" + msg);

                getNotiveList(1, 3);
            }
        });
    }

    /**
     * 提交人脸识别信息
     */
    public void saveFaceInfo(String dllx, String policeId) {
        addDisposable(restApi.saveCheckPolice(dllx, policeId), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel model = (BaseModel) o;
                DayAndMonthApplication.getInstance().logger.debug(">>>mjdljlb/saveCheckPolice人脸识别:" + GsonUtils.getInstance().toJson(model));
                baseView.saveFaceInfo(GsonUtils.getInstance().toJson(model));
            }

            @Override
            public void onError(String msg) {
                //baseView.showError(msg);
                DayAndMonthApplication.getInstance().logger.debug(">>>mjdljlb/saveCheckPolice失败:" + msg);
            }
        });
    }

    /**
     * 首页人脸是被信息
     * getFirstPageFceList
     */
    public void getFirstPageFceList() {
        addDisposable(restApi.getFirstPageFceList(new FirstPageFaceListBean(1, 24)), new BaseObserver(baseView) {
            @Override
            public void onSuccess(Object o) {
                BaseModel<FirstPageFaceCallBackBean> model = (BaseModel<FirstPageFaceCallBackBean>) o;
                baseView.getFirstPageFceList(model.getData());
            }

            @Override
            public void onError(String msg) {
                DayAndMonthApplication.getInstance().logger.debug(">>>mjdljlb/getList失败:" + msg);
            }
        });
    }

}
